//
//  SQGoodsPayViewController.m
//  pai
//
//  Created by pro on 14-11-21.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQGoodsPayViewController.h"

@interface SQGoodsPayViewController ()

@end

@implementation SQGoodsPayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self setTitle:@"兑换"];
    [self showGoods];
}

-(void)showGoods
{
    _scrollView = [[UIScrollView alloc]init];
    //[_scrollView setBackgroundColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.5]];
    _scrollView.frame = CGRectMake(0, self.MyNavigationBar.frame.origin.y+self.MyNavigationBar.frame.size.height, 320, [[UIScreen  mainScreen] bounds].size.height - 44 - self.MyNavigationBar.frame.origin.y-self.MyNavigationBar.frame.size.height);
    _scrollView.pagingEnabled = NO;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    
    self.goodsView = [SQGoodsView initmyview];
    self.goodsView.frame = CGRectMake(0, 0, 320, 350);
    [self.goodsView setItem:self.skuitem];
    [_scrollView addSubview:self.goodsView];
    
    [self.goodsView setDic:nil];
    
    self.addressSeted = false;
    
    self.addressButton = [[UIButton alloc]initWithFrame:CGRectMake(80, 360, 160, 40)];
    [self.addressButton setBackgroundColor:[UIColor redColor]];
    [self.addressButton setTitle:@"设置地址" forState:UIControlStateNormal];
    [self.addressButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.addressButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [self.addressButton addTarget:self action:@selector(onActionSetAddress:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:self.addressButton];
    
    
    //显示地址
    self.addressView = [SQGoodAddressView initmyview];
    self.addressView.frame = CGRectMake(0, 360, 320, 120);
    [_scrollView addSubview:self.addressView];
    [self.addressView setHidden:YES];
    UITapGestureRecognizer* singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickAddress:)];
    self.addressView.userInteractionEnabled=YES;
    [self.addressView addGestureRecognizer:singleTap];
    //支付按钮
    self.payButton = [[UIButton alloc]initWithFrame:CGRectMake(80, 500, 160, 40)];
    [self.payButton setBackgroundColor:[UIColor redColor]];
    [self.payButton setTitle:@"确认兑换" forState:UIControlStateNormal];
    [self.payButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.payButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [self.payButton addTarget:self action:@selector(onActionPay:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:self.payButton];
    [self.payButton setHidden:YES];
    
    
    _scrollView.contentSize = CGSizeMake(320,self.addressButton.frame.origin.y+self.addressButton.frame.size.height+20);
}
-(void)onActionSetAddress:(UIButton*)btn
{
    //[self addressSelected:nil];
    SQAddressListViewController *alvc = [[SQAddressListViewController alloc]init];
    alvc.delegate = nil;
    alvc.delegate =self;
    alvc.type = @"select";
    [self.navigationController pushViewController:alvc animated:YES];
}
-(void)addressSelected:(addr_t)address
{
    self.addr = address;
    //隐藏
    [self.addressButton removeFromSuperview];
    //显示
    [self.addressView setHidden:NO];
    [self.addressView setAddress:self.addr];
    [self.payButton setHidden:NO];
    
    _scrollView.contentSize = CGSizeMake(320,self.payButton.frame.origin.y+self.payButton.frame.size.height+20);
}
-(void)onClickAddress:(UITapGestureRecognizer *)gesture 
{
    SQAddressListViewController *alvc = [[SQAddressListViewController alloc]init];
    alvc.delegate = nil;
    alvc.delegate =self;
    alvc.type = @"select";
    alvc.selectedId = self.addr.address_id;
    [self.navigationController pushViewController:alvc animated:YES];
}
-(void)onActionPay:(id)sender
{
    biz_exchange_t exchange;
    exchange.base.msgtype = 8020;
    exchange.base.len = sizeof(exchange);
    exchange.base.error = 0;
    exchange.price = [self.goodsView.priceLabel.text intValue];
    exchange.points =[self.goodsView.pointLabel.text intValue];
    strcpy(exchange.username, [[Global instance].userinfo.username UTF8String]);
    strcpy(exchange.sku, self.skuitem.sku_code);
    exchange.addressid = self.addr.address_id;
    
    NSLog(@"%d",exchange.price);
    NSLog(@"%d",exchange.addressid);
    NSLog(@"%d",exchange.points);
    NSLog(@"%s",exchange.username);
    NSLog(@"%s",exchange.sku);
    
    NSMutableData* msgData = [NSMutableData data];
    [msgData appendBytes:&exchange length:sizeof(exchange)];
    
    [super sendData:msgData tag:1 withHUD:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//读取数据
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    if (tag == 1) {
        acs_base_t base;
        [data getBytes:&base length:sizeof(base)];
        NSString* msg = @"";
        NSLog(@"base.error:%d",base.error);
        if (base.error == ACS_SUCCESS) {
            //兑换成功
            msg = @"兑换成功";
        }else if (base.error == 3042)
        {
            msg = @"兑换出错，请重试";
        }else if (base.error == 3044)
        {
            msg = @"当前物品已售完";
        }else if (base.error == 3046)
        {
            msg = @"积分超限使用";
        }else if (base.error == 3048)
        {
            msg = @"你没有这么多得积分";
        }else if (base.error == 3032)
        {
            msg = @"你没有这么多游戏豆";
        }
        NSLog(@"%@",msg);
        [self.HUD showAnimated:YES whileExecutingBlock:^{
            self.HUD.labelText = msg;
            sleep(2);
            [self.HUD show:NO];
        } completionBlock:^{
            if (base.error == ACS_SUCCESS) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];

    }
}
@end
